package us.esme.managers
{
	import mx.collections.ArrayCollection;
	
	public class ContactsManager
	{
			
		private var followers:ArrayCollection;
		[Bindable] public var following:ArrayCollection;
		[Bindable] public var followersWithFollow:ArrayCollection;
		
		public function ContactsManager()
		{
			followers = new ArrayCollection();
			following = new ArrayCollection();
			followersWithFollow = new ArrayCollection();
		}
		
		public function storeFollowers(obj:Object):void{
		if(obj.esme_api){
			if(obj.esme_api.user){
				followers.removeAll();
				var userObject:Object = new Object();
				userObject = obj.esme_api.user;					
					if(userObject is ArrayCollection){
						for each ( var u:Object in userObject ){
								followers.addItem(u)
						}										
					}else{						
						followers.addItemAt(userObject.user,0);
					}	
				}
			}	
			
			getUsersToFollow(following, followers);	
		}
		
		public function storeFollowing(obj:Object):void{
		if(obj.esme_api){
			if(obj.esme_api.user){
				following.removeAll();
				var userObject:Object = new Object();
				userObject = obj.esme_api.user;				
					if(userObject is ArrayCollection){	
						for each ( var u:Object in userObject ){
							following.addItem(u)
						}										
					}else{
						following.addItemAt(userObject,0);
					}	
				}
			}
			
			getUsersToFollow(following, followers);		
		}
		
		private function getUsersToFollow(following:ArrayCollection, followers:ArrayCollection):void{
			followersWithFollow.removeAll();
			for each (var uFollowers:Object in followers){
				uFollowers.following = false;
				for each (var uFollowing:Object in following){
					if (uFollowing.nickname == uFollowers.nickname){
						uFollowers.following = true;
					}															
				}
				followersWithFollow.addItem(uFollowers);
			}
		}		
	}
}